package de.yellowshoes.gtugrm.googleplus.gae;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.Plus.People;
import com.google.api.services.plus.Plus.People.Search;
import com.google.api.services.plus.model.PeopleFeed;
import com.google.api.services.plus.model.Person;

public class StartServlet extends GooglePlusServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5930046336985310925L;

	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String scope = req.getParameter( REQUEST_PARM_SCOPE );
		
		if( StringUtils.equals(scope, REQUEST_VALUE_SCOPE_PEOPLE) ){
			doPeopleRequest(req, resp);
		}
		else if( StringUtils.equals(scope, REQUEST_VALUE_SCOPE_ACTIVITIES) ){
			doActivitiesRequest(req, resp);
		}
		else{
			resp.sendRedirect( PAGES_INDEX_HTML );
		}
	}

	@Override
	protected void doSubPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doSubGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}
	
	private void doPeopleRequest( HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String command = req.getParameter(REQUEST_PARM_COMMAND);
		
		// command set
		if( StringUtils.isNotBlank(command) ){
			// People search
			if( StringUtils.equals(command, REQUEST_VALUE_COMMAND_SEARCH) ){
				String query = req.getParameter(REQUEST_PARM_QUERY);
				
				// Start people search
				if( StringUtils.isNotBlank(query)){
					Plus plus = new Plus( new NetHttpTransport(), new JacksonFactory() );
					plus.setKey( API_KEY );
					plus.setOauthToken( accessToken );
					
					Search search = plus.people.search();
					search.setQuery(query);
					PeopleFeed peopleFeed = search.execute();
					
					if( peopleFeed.isEmpty() == false ){
						String template = readTemplate(PAGES_PEOPLE_SEARCH_HTML);
						
						StringBuffer content = new StringBuffer();
						content.append( "<ul>\n" );
						
						List<Person> peopleList = peopleFeed.getItems();
						
						if( peopleList != null ){
							for (Person person : peopleList) {
								content.append( "<li><a href=\"" );
								content.append( person.getUrl() );
								content.append( "\">" );
								content.append( person.getDisplayName() );
								content.append( "</a></li>\n" );
							}
						}
						content.append( "</ul>\n" );
						template = template.replace( "#content#", content.toString() );
						resp.getWriter().write( template );
					}
					else
					{
						resp.sendRedirect(PAGES_PEOPLE_HTML );
					}
				}
				else{
					resp.sendRedirect(PAGES_PEOPLE_HTML );
				}
			}
		}
		// show people startpage
		else{
			resp.sendRedirect( PAGES_PEOPLE_HTML );
		}
		
	}
	
	private void doActivitiesRequest( HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		
	}

}
